package com.webserver.core;

import com.webserver.http.EmptyRequestException;
import com.webserver.http.HttpRequest;

import java.io.*;
import java.net.Socket;

/**
 * 用于处理客户端的交互
 * 遵循HTTP协议与客户端的交互规则为一问一答,因此这里处理分为三步:
 * 1.解析请求
 * 2.处理请求
 * 3.响应客户端
 * 断开连接.
 */
public class ClientHandler implements Runnable {
    private Socket socket;

    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            //第一步:解析请求
            HttpRequest request = new HttpRequest(socket);

            //第二步:处理请求
            String path = request.getUri();//获取抽象路径
            System.out.println("抽象路径========> "+path);

            //第三步:响应客户端
            File file = new File("./webapps" + path);
            if (file.exists() && file.isFile()){


            /*
            HTTP/1.1 200 OK(CRLF)
            Content-Type: text/html(CRLF)
            Content-Length: 2546(CRLF)(CRLF)
            1011101010101010101......
             */
            OutputStream out = socket.getOutputStream();
            //3.1发送状态行
            String line = "HTTP/1.1 200 OK";
            byte[] data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);

            //3.2发送响应头
            line = "Content-Type: text/html";
            data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);

            line = "Content-Length: "+file.length();
            data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);
            //单独发送CRLF表示消息头部分发送完毕
            out.write(13);
            out.write(10);

            //3.3发送响应正文

            FileInputStream fis =new FileInputStream(file);
            int len;
            byte[] buf = new byte[1024 * 10];
            while ((len = fis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            }else {
                File file1 = new File("./webapps/root/404.html");
                OutputStream out = socket.getOutputStream();
                //3.1发送状态行
                String line = "HTTP/1.1 200 OK";
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                //3.2发送响应头
                line = "Content-Type: text/html";
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                line = "Content-Length: "+file1.length();
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);
                //单独发送CRLF表示消息头部分发送完毕
                out.write(13);
                out.write(10);

                //3.3发送响应正文

                FileInputStream fis =new FileInputStream(file1);
                int len;
                byte[] buf = new byte[1024 * 10];
                while ((len = fis.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
            }
            System.out.println("响应发送完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EmptyRequestException e) {
            //空请求什么都不用处理,目的是忽略上面的2,3步操作
        } finally {
            //交互完毕后断开连接(协议要求)
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}